package my.jbf.firstGame.MODULE;

public class ParsePassward
{

	private String step1_change2numbers(String a)
	{
		/*
		 加密原理:根据下面矩阵变换数字，然后使用用它除以质数，从2开始逐渐除以下一个，但最后剩4个数位置，最后面两位加上次数
		 //01234567
		 0  jAiBhCg
		 1 SkTzUyDf
		 2 Rl49VxEe
		 3 Qm38WwFd
		 4 Pn27XvGc
		 5 Oo16YuHb
		 6 Np05ZtIa
		 7 MqLrKsJ
		 */

		StringBuffer temp=new StringBuffer();
		temp.append('3');
		for (int i=0;i <= a.length() - 1;i++)
		{
			switch (a.charAt(i))
			{

				case 'A':temp.append( "02");break;
				case 'a':temp.append( "67");break;
				case 'B':temp.append( "04");break;
				case 'b':temp.append( "57");break;
				case 'C':temp.append( "06");break;
				case 'c':temp.append( "47");break;
				case 'D':temp.append( "16");break;
				case 'd':temp.append( "37");break;
				case 'E':temp.append( "26");break;
				case 'e':temp.append( "27");break;
				case 'F':temp.append( "36");break;
				case 'f':temp.append( "17");break;
				case 'G':temp.append( "46");break;
				case 'g':temp.append( "07");break;
				case 'H':temp.append( "56");break;
				case 'h':temp.append( "05");break;
				case 'I':temp.append( "66");break;
				case 'i':temp.append( "03");break;
				case 'J':temp.append( "76");break;
				case 'j':temp.append( "01");break;
				case 'K':temp.append( "74");break;
				case 'k':temp.append( "11");break;
				case 'L':temp.append( "72");break;
				case 'l':temp.append( "21");break;
				case 'M':temp.append( "70");break;
				case 'm':temp.append( "31");break;
				case 'N':temp.append( "60");break;
				case 'n':temp.append( "41");break;
				case 'O':temp.append( "50");break;
				case 'o':temp.append( "51");break;
				case 'P':temp.append( "40");break;
				case 'p':temp.append( "61");break;
				case 'Q':temp.append( "30");break;
				case 'q':temp.append( "71");break;
				case 'R':temp.append( "20");break;
				case 'r':temp.append( "73");break;
				case 'S':temp.append( "10");break;
				case 's':temp.append( "75");break;
				case 'T':temp.append( "12");break;
				case 't':temp.append( "65");break;
				case 'U':temp.append( "14");break;
				case 'u':temp.append( "55");break;
				case 'V':temp.append( "24");break;
				case 'v':temp.append( "45");break;
				case 'W':temp.append( "34");break;
				case 'w':temp.append( "35");break;
				case 'X':temp.append( "44");break;
				case 'x':temp.append( "25");break;
				case 'Y':temp.append( "54");break;
				case 'y':temp.append( "15");break;
				case 'Z':temp.append( "64");break;
				case 'z':temp.append( "13");break;
				case '0':temp.append( "62");break;
				case '1':temp.append( "52");break;
				case '2':temp.append( "42");break;
				case '3':temp.append( "32");break;
				case '4':temp.append( "22");break;
				case '5':temp.append( "63");break;
				case '6':temp.append( "53");break;
				case '7':temp.append( "43");break;
				case '8':temp.append( "33");break;
				case '9':temp.append( "23");break;
			}
		}
		//  Toast toast=Toast.makeText(getApplicationContext(),Integer.toString(m),2000);
		return temp.toString();
	}

	private String step2_changelength(String a)
	{//返回22位数字
		String outputString="";
		if (a.length() == 22)
		{
			return a;
		}
		if (a.length() > 22)
		{
			String temp1=a.substring(21);
			String temp2=a.substring(0, 21);
			char temp[]=temp2.toCharArray();
			boolean upflag=false;     
			for (int i=0;i < temp1.length();i++)
			{
				int change=temp1.charAt(temp1.length() - 1 - i) - '0' + temp2.charAt(temp2.length() - 1 - i) - '0';
				if (upflag)
				{
					change += 1;
					upflag = false;
				}
				if (change > 9)
				{
					upflag = true;
					change = change - 10;
				}
				temp[temp2.length() - i - 1] = (char)(change + 48);
			}
			if (upflag)
			{
				temp[temp2.length() - temp1.length()] ++;
				upflag = false;
			}
			outputString = String.valueOf(temp);
		}
		if (a.length() < 22)
		{
			int key[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
			outputString = a;
			int length=outputString.length();
			int i=0;
			while (outputString.length() < 21)
			{
				int temp=0;
				temp = outputString.charAt(length - 1 - i) - '0' + key[i];
				while (temp > 9)
				{
					if (temp >= 10)
					{
						temp = temp % 10 + temp / 10;
					}
				}
				i++;
				if (i >= 24)
				{
					i = 0;
				}
				outputString += temp;
			}
		}
		return outputString;
	}
	private String step3_change2string(String a)
	{
		//数字除以62，取余数匹配字母跟数字
		String back_string="";
		String temp_string="";
		temp_string = a;
		while (temp_string.length() > 2)
		{


			while (!temp_string.equals("0"))
			{
				String changing="";
				int x=0;
				int y=0;
				while (temp_string.length() > 2)
				{
					int temp_int=(temp_string.charAt(0) - '0') * 100
						+ (temp_string.charAt(1) - '0') * 10
						+ (temp_string.charAt(2) - '0');
					x = temp_int / 62;
					y = temp_int % 62;
					String temp=String.valueOf(y) + temp_string.substring(3);
					changing = changing + String.valueOf(x);
					temp_string = temp;
				}
				if (Integer.parseInt(temp_string) > 61)
				{
					changing += "1";
					back_string += number2char(String.valueOf(Integer.parseInt(temp_string) - 62));
				}
				else
				{
					changing += "0";
					back_string += number2char(String.valueOf(Integer.parseInt(temp_string)));
				}
				temp_string = changing;
			}
		}
		return back_string;
	}
	private String number2char(String a)
	{

		switch (Integer.valueOf(a))
		{
			case 0:
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
			case 9:return a;
			case 10:return "A";
			case 11:return "a";
			case 12:return "b";
			case 13:return "B";
			case 14:return "C";
			case 15:return "c";
			case 16:return "d";
			case 17:return "D";
			case 18:return "E";
			case 19:return "e";
			case 20:return "F";
			case 21:return "f";
			case 22:return "g";
			case 23:return "G";
			case 24:return "H";
			case 25:return "h";
			case 26:return "i";
			case 27:return "I";
			case 28:return "J";
			case 29:return "j";
			case 58:return "K";
			case 59:return "k";
			case 30:return "l";
			case 31:return "L";
			case 32:return "M";
			case 33:return "m";
			case 34:return "n";
			case 35:return "N";
			case 36:return "O";
			case 37:return "o";
			case 38:return "p";
			case 39:return "P";
			case 40:return "Q";
			case 41:return "q";
			case 42:return "r";
			case 43:return "R";
			case 44:return "S";
			case 45:return "s";
			case 46:return "t";
			case 47:return "T";
			case 48:return "U";
			case 49:return "u";
			case 50:return "v";
			case 51:return "V";
			case 52:return "w";
			case 53:return "W";
			case 54:return "X";
			case 55:return "x";
			case 56:return "y";
			case 57:return "Y";
			case 60:return "Z";
			case 61:return "z";
			default:return null;
		}

	}
	public String getkey(String a)
	{
		String str;
		str=step3_change2string(step2_changelength(step1_change2numbers(a)));
		return str;
	}
}

